package com.javaee.controller.user;

import com.javaee.BeforeBaseController;
import com.javaee.entity.Doctor;
import com.javaee.entity.Mzgh;
import com.javaee.entity.User;

import com.javaee.service.user.AppointmentService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class AppointmentController extends BeforeBaseController {
    @Autowired
    private AppointmentService appointmentService;
    @RequestMapping("/appointment")
    public String queryAppointmentList(Model model)
    {
        List<Doctor> appointmentList=appointmentService.findAll();
        model.addAttribute("appointmentList",appointmentList);
        return "/user/userindex";
    }
    @RequestMapping("/searchfromall")
    public String searchfromall(Model model,String mykey){
        model.addAttribute("appointmentList",appointmentService.searchfromall(mykey));
        return "/user/userindex";
    }
    @RequestMapping("/toAppointment")
    public String queryAppointmentList(Model model,int currentPage,int mykey,HttpSession session)
    {
//        定义字符串weekday变量，如果mykey为1，则weekday为星期一，为2，则weekday为星期二，以此类推
        String weekday = "";
        switch (mykey) {
            case 1:
                weekday = "Monday";
                break;
            case 2:
                weekday = "Tuesday";
                break;
            case 3:
                weekday = "Wednesday";
                break;
            case 4:
                weekday = "Thursday";
                break;
            case 5:
                weekday = "Friday";
                break;
            case 6:
                weekday = "Saturday";
                break;
            case 7:
                weekday = "Sunday";
                break;
        }
        session.setAttribute("weekday", weekday);
//        查询星期几医生数量
        int totalCount = appointmentService.selectCountByday(weekday);
        int perPageSize = 3;
        int totalPage = (int)Math.ceil(totalCount*1.0/perPageSize);
        List<Doctor> doctorList = appointmentService.findDoctorByPage((currentPage-1)*perPageSize, perPageSize,weekday);

        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("mykey",mykey);
        model.addAttribute("doctorList",doctorList);
        return "/user/appointment";
    }
    @RequestMapping("/appointmentDoctor")
    @ResponseBody
    public String appointmentDoctor(int dId,String time,HttpSession session){
        //查询医生信息
        Doctor doctor = appointmentService.findDoctorById(dId);
        //查询用户信息
        User user = (User)session.getAttribute("User");
        if(user.getBalance()<doctor.getFee()){
            return "余额不足";
        }
        //插入预约信息
        Mzgh mzgh = new Mzgh();
        mzgh.setDoctor_id(dId);
        mzgh.setDname(doctor.getName());
        mzgh.setU_id(user.getId());
        mzgh.setUname(user.getName());
        mzgh.setDetail((String) session.getAttribute("weekday"));
       mzgh.setYyrq(time);
       mzgh.setStatus("0");
        appointmentService.insertMzgh(mzgh);
        //修改排班表
        appointmentService.updatePlanReduce(dId,(String) session.getAttribute("weekday"));
        //修改用户余额

        appointmentService.ReduceMyBalance(user.getId(),doctor.getFee());
        return "预约成功";
    }
    @RequestMapping("/search")
    public String search(Model model,String key,HttpSession session){
        model.addAttribute("doctorList",
                appointmentService.search(key,   (String) session.getAttribute("weekday")));
        return "/user/appointment";
    }



    @RequestMapping("/selectMyMzghByPage")
    public String selectMyMzghByPage(Model model, Integer currentPage, HttpSession session) {
        if (currentPage == null) {
            currentPage = 1;
        }
        User user = (User)session.getAttribute("User");
        int Uid = user.getId();
        int totalCount = appointmentService.selectCount(Uid);
        int perPageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/perPageSize);

        List<Map<String, Object>> mzghList = appointmentService.selectMyMzghByPage((currentPage-1)*perPageSize, perPageSize, Uid);

        // 创建星期映射
        Map<String, String> weekdayMap = new HashMap<>();
        weekdayMap.put("Monday", "星期一");
        weekdayMap.put("Tuesday", "星期二");
        weekdayMap.put("Wednesday", "星期三");
        weekdayMap.put("Thursday", "星期四");
        weekdayMap.put("Friday", "星期五");
        weekdayMap.put("Saturday", "星期六");
        weekdayMap.put("Sunday", "星期日");
        model.addAttribute("weekdayMap", weekdayMap);
        model.addAttribute("mzghList",mzghList);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "user/selectMyMzghByPage";
    }
    @RequestMapping("/cancleMzgh")
    @ResponseBody
    public String cancleMzgh(Integer id,Integer dId,HttpSession session){
        System.out.println("id:"+id);
        System.out.println("dId:"+dId);
     //根据id查到星期几
        String weekday = appointmentService.findMzghById(id).getDetail();
        //删除我的预约
        appointmentService.deleteMzgh(id);
        //医生的预约数量加1，对plan表操作
        appointmentService.updatePlan(dId,weekday);
        //我的余额加
        User user = (User)session.getAttribute("User");
        Doctor doctor = appointmentService.findDoctorById(dId);
        user.setBalance(user.getBalance()+doctor.getFee());
        session.setAttribute("User",user);
        appointmentService.updateMyBalance(user.getId(), doctor.getFee());
        return "已取消";
    }
}
